<?php
	include_once("common.php");
	include_once("script/account_manager.php");

	# visitor
	if(isVisitor() && ! isset($_SESSIO['username']))
		header("Location: index.php");

	# editing other user
	if(!isAdmin($_SESSION['username']) && isset($_GET['id']))
		header("Location: account.php");

	if(sizeof($_POST) >= 6){
		$result = update($_POST);
	}

	if(! isset($_SESSION['idusers'])){
		$data = getNewUser($_SESSION['username']);
	}
	else{
		if(isset($_GET['id']))
			$id = $_GET['id'];
		else
			$id = $_SESSION['idusers'];
		$data = getUser($id);
	}

?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>COMP 353 Project</title>

    <!-- Bootstrap core CSS -->
    <link href="./css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="./css/template.css" rel="stylesheet">

    <!-- Just for debugging purposes. Don't actually copy this line! -->
    <!--[if lt IE 9]><script src="../../docs-assets/js/ie8-responsive-file-warning.js"></script><![endif]-->

    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
    <![endif]-->
  </head>

  <body>

    <div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
      <div class="container">
        <div class="navbar-header">
          <a class="brand" href="#">SEMS Lite</a>
        </div>
        <div class="navbar">
          <ul class="nav navbar-nav">
            <li><a href="index.php">Home</a></li>
            <li><a href="search.php">Search</a></li>
            <li><a href="signup.php">Signup</a></li>
          </ul>
        </div>
      </div>
    </div>

    <!-- Main jumbotron for a primary marketing message or call to action -->
<div class="jumbotron">
	<div class="container">
<?php
	if(isset($_SESSION['username']) && isAdmin($_SESSION['username']))
		include("script/admin_menu.php");

	if(!isVisitor())
		include_once("script/submenu.php");
?>
<?php
	if(isset($result)){
		if($result){
			echo '<div class="alert alert-success">',
				"Account successfully updated!",
				'</div>';
		}
		else{
			echo '<div class="alert alert-error">',
				"Invalid submission",
				'</div>';
		}
	}
?>
		<form class="form-horizontal" action="account.php" method="post">
			<legend>Account Settings</legend>
			<div class="center">
				<div class="control-group">
					<label class="control-label" for="inputUsername">Username</label>
					<div class="controls">
						<span name="username" id="inputUsername" class="input uneditable-input"><?php echo $data['username']; ?></span>
					</div>
				</div>
				<div class="control-group">
					<label class="control-label" for="inputPassword">Password</label>
					<div class="controls">
						<input name="password" type="password" id="inputPassword" placeholder="Password" value="<?php echo $data['password']; ?>" />
					</div>
				</div>
				<div class="control-group">
					<label class="control-label" for="inputEmail">Email</label>
					<div class="controls">
						<input name="email" type="text" id="inputEmail" placeholder="Email" value="<?php echo $data['email']; ?>" />
					</div>
				</div>
				<div class="control-group">
					<label class="control-label" for="inputFirstname">First name</label>
					<div class="controls">
						<input name="first_name" type="text" id="inputFirstname" placeholder="First name" value="<?php echo $data['first_name']; ?>" />
					</div>
				</div>
				<div class="control-group">
					<label class="control-label" for="inputLastname">Last name</label>
					<div class="controls">
						<input name="last_name" type="text" id="inputLastname" placeholder="Last name" value="<?php echo $data['last_name']; ?>" />
					</div>
				</div>
				<div class="control-group">
					<label class="control-label" for="inputCountry">Country</label>
					<div class="controls">
						<input name="country" type="text" autocomplete="off" data-provide="typeahead" data-items="4" id="inputCountry" placeholder="Country" value="<?php echo $data['country']; ?>" />
					</div>
				</div>
				<div class="control-group">
					<label class="control-label" for="inputOrganization">Organization</label>
					<div class="controls">
						<input name="organization" type="text" autocomplete="off" data-provide="typeahead" data-items="4" id="inputOrganization" placeholder="Organization" value="<?php echo $data['organization']; ?>" />
					</div>
				</div>
				<div class="control-group">
					<label class="control-label" for="inputDepartment">Department</label>
					<div class="controls">
						<input name="department" type="text" id="inputDepartment" placeholder="Department" value="<?php echo $data['department']; ?>" />
					</div>
				</div>
				<div class="control-group">
					<label class="control-label" for="inputHomepage">Default Homepage</label>
					<div class="controls">
						<span name="homepage" id="inputHomepage" class="input uneditable-input"><?php echo $data['home']; ?></span>
					</div>
				</div>
				<div class="control-group">
					<div class="controls">
						<button type="submit" class="btn btn-primary">Save</button>
						<button id="reset_home" class="btn offset1">Reset Homepage</button>
					</div>
				</div>
			</div>
		</form>
	</div>
</div>
<div class="container">
	<legend>Interest</legend>
<?php
# This part cannot work on local since it need the Concordia MYSQL.

#	$c = connect();
#	$r = mysql_query("SELECT shid  FROM interest WHERE users_idusers = '".$_SESSION['idusers']."'");
#	$ints = array();
#	while($row = mysql_fetch_array($r))
#		array_push($ints, $row[0]);
#	mysql_close($c);
#	$c = connectToExternalDB();
#	$r = mysql_query("SELECT * FROM sh WHERE level=1");
#	$root = array();
#	while($row = mysql_fetch_assoc($r)){
#		$root[$row['shid']] = array ("data"=>$row, "child"=>array());
#	}
#
#	foreach($root as $f){
#		$r = mysql_query("SELECT * FROM sh WHERE level=2 AND parentid=\"".$f['data']['shid'].'"');
#		while($row = mysql_fetch_assoc($r)){
#			$root[$f['data']['shid']]['child'][$row['shid']] = array("data"=>$row, "child"=>array());
#		}
#		mysql_free_result($r);
#	}
#
#	foreach($root as $f){
#		foreach($f['child'] as $g){
#			$r =mysql_query("SELECT * FROM sh WHERE level=3 AND parentid=\"".$g['data']['shid'].'"');
#			while($row = mysql_fetch_assoc($r)){
#				$root[$f['data']['shid']]['child'][$g['data']['shid']]['child'][$row['shid']] = $row;
#			}
#			mysql_free_result($r);
#		}
#	}
#	mysql_close($c);
#
#	$id="";
#	if(isset($_GET['id']))
#		$id="data-id=\"".$id."\"";
#	foreach($root as $field){
#		echo '<div class="field accordion">'."\n";
#		echo '<h3>'.$field['data']['cname'].'</h3>'."\n";
#		foreach($field['child'] as $shid2 => $group){
#			echo '<div class="group accordion-group" id="'.$group['data']['shid'].'">'."\n";
#			if(sizeof($group['child'])>0){
#				echo '<h4 class="accordion-toggle collapsed" data-toggle="collapse" data-target="#'.$group['data']['shid'].'-content">'.$group['data']['cname']."</h4>\n",
#					'<div id="'.$group['data']['shid'].'-content" class="collapse">',
#					'<table class="table table-condensed table-hover">';
#				foreach($group['child'] as $sub){
#					echo "<tr>\n",
#						'<td><input class="chk-interest" type="checkbox" '.$id.' value="'.$sub['shid'].'" /></td><td>'.$sub['cname'].'</td>'."\n",
#						"</tr>\n";
#				}
#				echo "</table>\n</div>";
#			}
#			else
#				echo '<h4 class="accordion-toggle collapsed muted" >'.$group['data']['cname']."</h4>\n";
#			echo "</div>\n";
#		}
#		echo '</div>';
#	}
?>
		</table>
	</div>
</div>
    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
	<!-- CDN -->
	<!-- <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> -->
	<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
	<!-- LOCAL -->
    <script src="./js/jquery-1.10.2.min.js"></script>
	<script src="./js/bootstrap.min.js"></script>
	<script type="text/javascript">
		function lookupOrganization(txt, callback){
			var country = $("#inputCountry").val();
			var d = null;
			if(country != ""){
				d = {org: txt, country: country};
			}
			else{
				d = {org: txt};
			}
			$.ajax({url: "script/organization.php", data: d})
				.done(function(response){
					callback(response.split(","));
				});
		}

		function lookupCountry(txt, callback){
			$.ajax({url: "script/country.php", data: {country: txt}})
				.done(function(response){
					callback(response.split(","));
				});
		}

		$("#inputCountry").typeahead({source: lookupCountry})
		$("#inputOrganization").typeahead({source: lookupOrganization});

		$("#reset_home").click(function(){
			$.ajax({url: "script/set_homepage.php"})
				.done(function(response){
					$("#inputHomepage").text("");
				});
			return false;
		});
		$(".chk-interest").click(function(){
			var ck = $(this);
			var data = {};
			if(ck.attr('data-id') != null)
				data = {shid: ck.val(), id: ck.attr("data-id"), state: ck.prop("checked")};
			else
				data = {shid: ck.val(), state: ck.prop("checked")};
			$.ajax({url: "script/set_interest.php", data: {shid: ck.val()}});
		});
		$(".submenu .nav li:nth-child(1)").addClass("active");
	</script>
  </body>
</html>
